<?php

namespace app\admin\controller\shop;

use app\common\controller\Backend;
use app\index\controller\Pdf;
use think\Db;
/**
 * 提分超市-定点合同管理
 *
 * @icon fa fa-circle-o
 */
class Contract extends Backend
{
    
    /**
     * Contract模型对象
     * @var \app\admin\model\shop\Contract
     */
    protected $model = null;
    protected $searchFields = 'id,order_id,userid,id_card,student_name,parent_name,phone';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\shop\Contract;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("genderList", $this->model->getGenderList());
        $this->view->assign("editionList", $this->model->getEditionList());
        $this->view->assign("statusList", $this->model->getStatusList());
     
    }
     /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        
        $row = $this->model->where(['id'=>$ids])->find();
        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $params['z_publish'] = '11';
             Db::startTrans();
            if ($params) {
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->Pdf = new Pdf();
                        $info = $row->toArray();
                        $resu = $this->Pdf->watermark($info,1);
                       if($resu['true'] == 1 && $info['status'] == 2){
                            $params['contract_url'] = $resu['data'];
                            $resu = $this->Pdf->watermark($info,2);
                           if($resu['true'] != 1){
                                Db::rollback();
                                $this->error('操作失败');
                           }
                           $params['contract_all_url'] = $resu['data'];
                           $resu = $this->Pdf->watermark($info,3);
                           if($resu['true'] != 1){
                                Db::rollback();
                                $this->error('操作失败');
                           }
                           $params['contract_blank_url'] = $resu['data'];
                           $result = $row->allowField(true)->save($params);
                            if($result){
                                Db::commit();
                                $this->success('操作成功');
                            }else{
                                Db::rollback();
                                $this->error('操作失败');
                            }
                       }else{
                         Db::rollback();
                        $this->error('操作失败');
                       }
                    } else {
                        Db::rollback();
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                     Db::rollback();
                    $this->error($e->getMessage());
                }
            }
               Db::rollback();
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
     /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk['0'], 'in', $ids)->select();
               $count = 0;
            foreach ($list as $k => $v) {
                $count += $this->model->where($pk['0'], '=', $v['id'])->delete();
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
}
